package nostalgia;

import javax.swing.*;
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class UserConfiguration extends Properties {
    public static final String LOCAL_COLLECTION_TYPE_DIRECTORY = "dir";
    private static UserConfiguration instance;
    private File file;

    public static synchronized UserConfiguration getInstsance() {
        if(instance == null) {
            instance = new UserConfiguration();
        }
        return instance;
    }

    private UserConfiguration() {
        this.file = new File(SystemConfiguration.getInstsance().getUserDataDirectory(), "configuration.properties");
        load();
    }

    public void load() {
        if(file.exists()) {
            super.clear();
            try {
                super.load(new FileInputStream(file));
            }
            catch(IOException e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(null, "Error reading user configuration.");
            }
        }
    }

    public void save() throws IOException {
        super.store(new FileOutputStream(file), "");
    }

    public boolean exists() {
        return file.exists();
    }

    public String getLastFmUsername() {
        return getProperty("lastfm.username", "");
    }

    public void setLastFmUsername(String value) {
        setProperty("lastfm.username", value);
    }

    public String getLastFmPassword() {
        return getProperty("lastfm.password", "");
    }

    public void setLastFmPassword(String value) {
        setProperty("lastfm.password", value);
    }

    public String getLocalCollectionType() {
        return getProperty("collection.type", LOCAL_COLLECTION_TYPE_DIRECTORY);
    }

    public void setLocalCollectionType(String value) {
        setProperty("collection.type", value);
    }

    public String getLocalCollectionDirectory() {
        return getProperty("collection.dir", "");
    }

    public void setLocalCollectionDirectory(String value) {
        setProperty("collection.dir", value);
    }

}
